<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
		<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
		<title>我的购物车</title>
		<link rel="icon" href="./img/favicon.ico">

		<!-- 引入样式 -->
		<link rel="stylesheet" type="text/css" href="./css/style.css" />
	</head>

	<body>
		<!--head-->
		<!-- 头部栏位 -->
		<!--页面顶部-->
		<div id="nav-bottom">
			<!--顶部-->
			<div class="nav-top">
				<div class="top">
					<div class="py-container">
						<div class="shortcut">
							<ul class="fl">
								<li class="f-item">shop欢迎您！</li>
								<li class="f-item">请
									<a href="login.html" target="_blank">登录</a>
									<span>
										<a href="register.html" target="_blank">免费注册</a>
									</span>
								</li>
							</ul>
							<div class="fr typelist">
								<ul class="types">
									<li class="f-item">
										<span>我的订单</span>
									</li>

									<li class="f-item">
										<span>
											<a href="cart.html" target="_blank">我的购物车</a>
										</span>
									</li>
									<li class="f-item">
										<span>
											<a href="home.html" target="_blank">我的shop</a>
										</span>
									</li>
									<li class="f-item">
										<span>shop会员</span>
									</li>
									<li class="f-item">
										<span>企业采购</span>
									</li>
									<li class="f-item">
										<span>关注shop</span>
									</li>
									<li class="f-item">
										<span>
											<a href="cooperation.html" target="_blank">合作招商</a>
										</span>
									</li>
									<li class="f-item">
										<span>
											<a href="shoplogin.html" target="_blank">商家后台</a>
										</span>
									</li>
									<li class="f-item">
										<span>网站导航</li>
								</ul>
							</div>

						</div>
					</div>
				</div>

				<!--头部-->
				<div class="header">
					<div class="py-container">
						<div class="yui3-g Logo">
							<div class="yui3-u Left logoArea">
								<a class="logo-bd" title="shop" href="index.html" target="_blank"></a>
							</div>
							<div class="yui3-u Rit searchArea">
								<div class="search">
									<form action="" class="sui-form form-inline">
										<!--searchAutoComplete-->
										<div class="input-append">
											<input type="text" id="autocomplete" class="input-error input-xxlarge" />
											<button class="sui-btn btn-xlarge btn-danger" type="button">搜索</button>
										</div>
									</form>
								</div>
							</div>

						</div>

					</div>
				</div>
			</div>
		</div>


		<div class="cart py-container">
			<!--All goods-->
			<div class="allgoods" id="app">
				<h4>全部商品
					<span></span>
				</h4>
				<div class="cart-main">
					<div class="cart-item-list">
						<div class="cart-body">
							<!-- vue loadlist -->
							<div class="cart-list" v-for="(item,index) in carts" :key="index">
								<ul class="goods-list yui3-g">
									<li class="yui3-u-1-24" style="border: none;">
										<input :value="index"  type="checkbox"  v-model="ids" @click="select(index)">
									</li>
									<li class="yui3-u-6-24" style="border: none;">
										<div class="good-item">
												<img :src="item.image" style="max-height: 80px;" />
										</div>
									</li>
									<li class="yui3-u-5-24" style="border: none;">
										{{item.name}}
									</li>
									<li class="yui3-u-1-8" style="border: none;">
										<span class="price">{{item.price}}</span>
									</li>
									<li class="yui3-u-1-8" style="border: none;">
										<div class="cart_icon">
											<a @click="add(item,-1)">-</a>
											<input type="text" v-model="item.num" minnum="1"  />
											<a @click="add(item,1)">+</a>
										</div>
									</li>
									<li class="yui3-u-1-8" style="border: none;">
										<span class="sum">125.00</span>
									</li>
									<li class="yui3-u-1-8 item-msg" style="border: none;">
										<a href="#none" @click="remove(item._id)">删除</a>
										<br />
										<a href="#none">移到收藏</a>
									</li>
								</ul>
							</div>

						</div>
					</div>
				</div>
				<div class="cart-tool">
					<div class="select-all">
						<input class="chooseAll" type="checkbox" v-model="allChecked" @change="toggleAll"/>
						<span>全选</span>
					</div>
					<div class="option">
						<a href="#none">删除选中的商品</a>
						<a href="#none">移到我的关注</a>
						<a href="#none">清除下柜商品</a>
					</div>
					<div class="money-box">

						<div class="chosed">已选择
							<span>0</span>件商品</div>
						<div class="sumprice">
							<span>
								<em>总价（不含运费） ：</em>
								<i class="summoney">¥{{allMoney()}}</i>
							</span>
							<span>
								<em>已节省：</em>
								<i>-¥20.00</i>
							</span>
						</div>
						<div class="sumbtn">
							<a class="sum-btn" @click="toOrder()">结算</a>
						</div>
					</div>
				</div>
				<div class="clearfix"></div>
				
			</div>
		</div>
		<!-- 底部栏位 -->
		<!--页面底部-->
		<div class="clearfix footer">
			<div class="py-container">
				<div class="footlink">
					<div class="Mod-service">
						<ul class="Mod-Service-list">
							<li class="grid-service-item intro  intro1">

								<i class="serivce-item fl"></i>
								<div class="service-text">
									<h4>正品保障</h4>
									<p>正品保障，提供发票</p>
								</div>

							</li>
							<li class="grid-service-item  intro intro2">

								<i class="serivce-item fl"></i>
								<div class="service-text">
									<h4>正品保障</h4>
									<p>正品保障，提供发票</p>
								</div>

							</li>
							<li class="grid-service-item intro  intro3">

								<i class="serivce-item fl"></i>
								<div class="service-text">
									<h4>正品保障</h4>
									<p>正品保障，提供发票</p>
								</div>

							</li>
							<li class="grid-service-item  intro intro4">

								<i class="serivce-item fl"></i>
								<div class="service-text">
									<h4>正品保障</h4>
									<p>正品保障，提供发票</p>
								</div>

							</li>
							<li class="grid-service-item intro intro5">

								<i class="serivce-item fl"></i>
								<div class="service-text">
									<h4>正品保障</h4>
									<p>正品保障，提供发票</p>
								</div>

							</li>
						</ul>
					</div>
					<div class="clearfix Mod-list">
						<div class="yui3-g">
							<div class="yui3-u-1-6">
								<h4>购物指南</h4>
								<ul class="unstyled">
									<li>购物流程</li>
									<li>会员介绍</li>
									<li>生活旅行/团购</li>
									<li>常见问题</li>
									<li>购物指南</li>
								</ul>

							</div>
							<div class="yui3-u-1-6">
								<h4>配送方式</h4>
								<ul class="unstyled">
									<li>上门自提</li>
									<li>211限时达</li>
									<li>配送服务查询</li>
									<li>配送费收取标准</li>
									<li>海外配送</li>
								</ul>
							</div>
							<div class="yui3-u-1-6">
								<h4>支付方式</h4>
								<ul class="unstyled">
									<li>货到付款</li>
									<li>在线支付</li>
									<li>分期付款</li>
									<li>邮局汇款</li>
									<li>公司转账</li>
								</ul>
							</div>
							<div class="yui3-u-1-6">
								<h4>售后服务</h4>
								<ul class="unstyled">
									<li>售后政策</li>
									<li>价格保护</li>
									<li>退款说明</li>
									<li>返修/退换货</li>
									<li>取消订单</li>
								</ul>
							</div>
							<div class="yui3-u-1-6">
								<h4>特色服务</h4>
								<ul class="unstyled">
									<li>夺宝岛</li>
									<li>DIY装机</li>
									<li>延保服务</li>
									<li>购物卡</li>
									<li>会员充值卡</li>
								</ul>
							</div>
							<div class="yui3-u-1-6">
								<h4>帮助中心</h4>
								<ul class="unstyled">
									<li>夺宝岛</li>
									<li>DIY装机</li>
									<li>延保服务</li>
									<li>购物卡</li>
									<li>会员充值卡</li>
								</ul>
							</div>
						</div>
					</div>
					<div class="Mod-copyright">
						<ul class="helpLink">
							<li>关于我们
								<span class="space"></span>
							</li>
							<li>联系我们
								<span class="space"></span>
							</li>
							<li>关于我们
								<span class="space"></span>
							</li>
							<li>商家入驻
								<span class="space"></span>
							</li>
							<li>营销中心
								<span class="space"></span>
							</li>
							<li>友情链接
								<span class="space"></span>
							</li>
							<li>关于我们
								<span class="space"></span>
							</li>
							<li>营销中心
								<span class="space"></span>
							</li>
							<li>友情链接
								<span class="space"></span>
							</li>
							<li>关于我们</li>
						</ul>
						
						<p>Copyright©2017-2020 All Rights Reserved.</p>
					</div>
				</div>
			</div>
		</div>
		<!--页面底部END-->
		
		<!-- 引入组件库 -->
		<script src="js/vue3.js"></script>
		<script src="js/axios.js"></script>
		<script src="js/request.js"></script>
		<script src="js/common.js"></script>
		<script>
			const {
				createApp
			} = Vue.createApp({
				data() {
					return {
						carts: [],
						ids:[],
						allChecked:false
					}
				},
				
				mounted(){
					//列表加载
					this.list();
				},
				methods:{
					
					//购物车列表加载
					list(){
						
						if(sessionStorage.getItem("userInfo")){
							let json = sessionStorage.getItem("userInfo")
							let userInfo = JSON.parse(json)
						    request.get(MALL_CART_BASEURL + '/cart/list/' + userInfo.userId)
							.then(resp =>{
								if(resp.code == 2000){
									this.carts=resp.data;
								}else if(resp.code == 3003 || resp.code == 2001){
									location.href = 'login.html'
								}
							})
						}else{
					      location.href = 'login.html'
				        }
					},
					//修改购物车
					add(item,count){
						//数量递减
						item.num=item.num+count;
					},
					//总金额计算
					allMoney(){
						let moneys = 0.0;
						for(let i=0;i<this.carts.length;i++){
							moneys+=this.carts[i].num*this.carts[i].price
						}
						return moneys;
					},
					//结算
					toOrder(){
						if(this.ids.length>0){
							let orders = []
							for(let i=0;i<this.ids.length;i++){
								orders.push(this.carts[this.ids[i]])
							}
							sessionStorage.setItem("orders",JSON.stringify(orders))
							location.href="order.html"
						}else{
							alert('请至少选择一个商品！')
						}
					},
					// 删除
					remove(id){
						request.delete(MALL_CART_BASEURL + '/cart/' + id)
						.then(resp =>{
						  this.list()
						})
					},
					// 选择商品
					select(index){
						let flag = 1
						for(let i = 0; i < this.ids.length; i++){
							if(this.ids[i] == index){
								this.ids.splice(i,1)
								flag = 0
								break
							}
						}
						if(flag){
							this.ids.push(index)
						}
						this.allChecked = this.ids.length == this.carts.length

					},
					// 切换全选状态的方法
					toggleAll() {
					  this.allChecked = this.ids.length == this.carts.length
					  if (!this.allChecked) {
						 // 全选时，选中所有项
						 for(let i = 0; i < this.carts.length; i++){
							 this.ids.push(i)
						 } 
						
						this.allChecked = true
					  } else {
						this.ids = []; // 取消全选时，清空选中项

						this.allChecked = false
					  }
					},
				}
			}).mount('#app')
		</script>
	</body>
</html>
